<%@page import="java.io.OutputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
/*
String root = request.getServletContext().getRealPath("/mboardfile");
//String savepath = "C:/Users/Administrator/Desktop/java/Web_semi/WebContent/mboardfile/";

String filename=request.getParameter("attfile");

String orgfilename = filename;

InputStream in = null;
OutputStream os =null;
File file = null;
//String client="";

try{
	//스트림에 담기
	file = new File(root,filename);
	in = new FileInputStream(file);*/
	/*
	client = request.getHeader("User-Agent");
	//다운 헤더 설정
	response.reset();
	response.setContentType("application/octet-stream");
	response.setHeader("Content-Description", "JSP Generated Data");
	//익스플로러 처리
	if(client.indexOf("MSIE")!=-1){
		response.setHeader ("Content-Disposition", "attachment; filename="+new String(orgfilename.getBytes("KSC5601"),"ISO8859_1")); 
	}else{
	//한글 파일 처리
		
		response.setHeader("Content-Disposition", "attachment; filename=\"" + orgfilename + "\"");
        response.setHeader("Content-Type", "application/octet-stream; charset=utf-8");
	}*/
	//orgfilename = new String(orgfilename.getBytes("utf-8"),"iso-8859-1");
	//response.setHeader ("Content-Length", ""+file.length() );
	/*
	os = response.getOutputStream();
    byte b[] = new byte[(int)file.length()];
    int leng = 0;
     
    while( (leng = in.read(b)) > 0 ){
        os.write(b,0,leng);
    }
    
    in.close();
    os.close();
	
}catch(Exception e){
	e.printStackTrace();
}
*/
try{

String name = request.getParameter("attfile");
String path ="/mboardfile";
ServletContext context = getServletContext();
String downpath = context.getRealPath(path);
String filepath = downpath+"/"+name;

byte b[] = new byte[4096];
File file = new File(filepath);

FileInputStream fis = new FileInputStream(file);

String mimetype = getServletContext().getMimeType(filepath);
System.out.println("mimetype = "+mimetype);

if(mimetype==null){
	mimetype="application/octet-stream";
	response.setContentType(mimetype);
	String sencoding = new String(name.getBytes("utf-8"),"iso-8859-1");
	response.setHeader("Content-Disposition","attachment; filename="+sencoding);
	
	ServletOutputStream outs = response.getOutputStream();
	int numread;
	System.out.println("뭐야1;");
	while((numread=fis.read(b, 0, b.length))!=-1){
		outs.write(b, 0, numread);
	}
	System.out.println("뭐야2;");
	outs.flush();
	outs.close();
	fis.close();
	
}
}catch(Exception e){
	out.print("<script>alert('오류');</script>");
}

out.print("<script>location.href=history.go(-1)();</script>");
%>

</body>
</html>